package com.eddie.libray.reception;

import com.eddie.libray.LibraryApp;
import com.eddie.libray.LibraryDataSource;
import com.eddie.libray.service.UserService;

import static com.eddie.libray.MenuConstant.USER_MENU_RETURN;

/**
 * 用户的请求接收器
 *
 * @author Eddie
 * @since 2022/09/02 11:57
 **/
public class UserReception extends AbstractReception {

    private static UserService userService;

    /**
     * 定义构造函数用于子状态之间的图书馆应用传递
     *
     * @param app 图书馆应用
     */
    public UserReception(LibraryApp app) {
        super(app);
    }

    @Override
    protected boolean action() {
        //初始化userService
        userService = new UserService(LibraryDataSource.users.get(LibraryDataSource.loginUser.getName()));
        menu.userMenu();
        String answer = scanner.next();
        switch (answer) {
            case "1": {
                userService.list();
                break;
            }
            case "2": {
                userService.borrowBook();
                break;
            }
            case "3": {
                userService.returnBook();
                break;
            }
            case USER_MENU_RETURN: {
                //设置图书馆系统的接收器为主菜单接收器
                app.setReceptionState(app.getMainReception());
                //返回false表示不在循环此菜单
                return false;
            }
            default: {
                break;
            }
        }
        return true;
    }
}
